<?php
class Convention
{
	private $m_id;
	private $m_name;
	private $m_site;
	private $m_startTime;
	private $m_endTime;
	private $m_eventSubmit;
	private $m_preReg;
	private $m_onsiteReg;
	private $m_active;
	
	public function Convention($p_name, $p_site, $p_startTime, $p_endTime, $p_id = -1)
	{
		// Should probably do some input checking here, to make sure
		// we're getting what we want
	
		if ($p_id >= 0)
		{
			$this->m_id = $p_id;
		}
		
		$this->m_name = $p_name;
		$this->m_site = $p_site;
		$this->m_startTime = $p_startTime;
		$this->m_endTime = $p_endTime;
		$this->m_eventSubmit = false;
		$this->m_preReg = false;
		$this->m_onsiteReg = false;
		$this->m_active = true;
	}
	
	public function getID()
	{
		return $this->m_id;
	}
	
	public function getName()
	{
		return $this->m_name;
	}
	
	public function setName($p_name)
	{
		// Should check input before setting
		$this->m_name = $p_name;
		
		return true;
	}
	
	public function getSite()
	{
		return $this->m_site;
	}
	
	public function setSite($p_site)
	{
		$this->m_site = $p_site;
	}
	
	public function getStartTime($p_format = DEFAULT_DATE_FORMAT)
	{
		return $this->m_startTime->format($p_format);
	}
	
	public function setStartTime($p_startTime)
	{
		if (is_string($p_startTime))
		{
			$this->m_startTime = new DateTime($p_startTime);
		}
		else if (is_object($p_startTime) && get_class($p_startTime) == 'DateTime')
		{
			$this->m_startTime = $p_startTime;
		}
		else
		{
			return false;
		}
		
		return true;
	}
	
	public function getEndtime($p_format = DEFAULT_DATE_FORMAT)
	{
		return $this->m_endTime->format($p_format);
	}
	
	public function setEndTime($p_endTime)
	{
		if (is_string($p_endTime))
		{
			$this->m_endTime = new DateTime($p_endTime);
		}
		else if (is_object($p_endTime) && get_class($p_endTime) == 'DateTime')
		{
			$this->m_endTime = $p_endTime;
		}
		else
		{
			return false;
		}
		
		return true;
	}
	
	public function eventSubmit()
	{
		return $this->m_eventSubmit;
	}
	
	public function setEventSubmit($p_eventSubmit)
	{
		if (is_bool($p_eventSubmit))
		{
			$this->m_eventSubmit = $p_eventSubmit;
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public function preReg()
	{
		return $this->m_preReg;
	}
	
	public function setPreReg($p_preReg)
	{
		if (is_bool($p_preReg))
		{
			$this->m_preReg = $p_preReg;
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public function onsiteReg()
	{
		return $this->m_onsiteReg;
	}
	
	public function setOnsiteReg($p_onsiteReg)
	{
		if (is_bool($p_onsiteReg))
		{
			$this->m_onsiteReg = $p_onsiteReg;
		}
		else
		{
			return false;
		}
	}
	
	public function isActive()
	{
		return $this->m_active;
	}
	
	public function setActive($p_active = true)
	{
		if (is_bool($p_active))
		{
			$this->m_active = $p_active;
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public function save()
	{
		$startString = $this->m_startTime->format('c');
		$endString = $this->m_endTime->format('c');
		$eventSubmit = ($this->m_eventSubmit) ? 'TRUE' : 'FALSE';
		$preReg = ($this->m_preReg) ? 'TRUE' : 'FALSE';
		$onsiteReg = ($this->m_onsiteReg) ? 'TRUE' : 'FALSE';
		$active = ($this->m_active) ? 'TRUE' : 'FALSE';
	
		if (isset($this->m_id) && $this->m_id >= 0)
		{
			$update = 'UPDATE convention SET '.
				"con_name = '".$this->m_name."', site = '".$this->m_site."', ".
				"start_time = '".$startString."', end_time = '".$endString."', ".
				'event_submit = '.$eventSubmit.', prereg = '.$preReg.', '.
				'onsite_reg = '.$onsiteReg.', active = '.$active;
			$result = pg_query($update);
			
			if ($result)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			$insert = 'INSERT INTO convention (con_name, site, start_time, end_time, event_submit, prereg, onsite_reg, active) '.
				"VALUES ('".$this->m_name."', '".$this->m_site."', '".$startString."', '".$endString."', ".$eventSubmit.', '.$preReg.
				', '.$onsiteReg.', '.$active.')';
			$result = pg_query($insert);
			
			if ($result)
			{
				$getID = "SELECT currval('org_counter') AS last_id";
				$result = pg_query($getID);
				$lastID = pg_fetch_assoc($result);
				$lastID = $lastID['last_id'];
				
				$this->m_id = $lastID;
				
				return true;
			}
			else
			{
				return false;
			}
		}
	}
	
	public static function getConventionByID($p_id)
	{
		if (!is_int($p_id))
		{
			return null;
		}
		
		$query = 'SELECT con_id, con_name, site, start_time, end_time, event_submit, prereg, onsite_reg, active '.
			"FROM convention WHERE con_id = '".$p_id."'";
		$result = pg_query($query);
		
		if ($result)
		{
			$cons = pg_fetch_all($result);
			
			if (count($cons) == 1)
			{
				$con = $cons[0];
				date_default_timezone_set('America/New_York');
				
				$startTime = new DateTime($con['start_time']);
				$endTime = new DateTime($con['end_time']);
				$newConvention = new Convention($con['con_name'], $con['site'], $startTime, $endTime, $con['con_id']);
				$newConvention->setEventSubmit($con['event_submit']);
				$newConvention->setPreReg($con['prereg']);
				$newConvention->setOnsiteReg($con['onsite_reg']);
				$newConvention->setActive($con['active']);
				
				return $newConvention;
			}
			else
			{
				return null;
			}
		}
		else
		{
			return null;
		}
	}
}
?>